home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / parse.l < prev    next >
Encoding:
Text File  |  1996-02-24  |  13.8 KB  |  257 lines

  1. D                       [0-9]
  2. L                       [a-zA-Z_]
  3. H                       [a-fA-F0-9]
  4. E                       [Ee][+-]?{D}+
  5. FS                      (f|F|l|L)
  6. IS                      (u|U|l|L)*
  7.  
  8. %x COMMENT COMMENT_START SPECIAL_COMMENT
  9. %x CPP CPP_START
  10. %x CPP_INC_FILE CPP_INC_FLAGS
  11. %x CPP_DEFINE CPP_DEFINE_ARGP CPP_DEFINE_BODY CPP_DEFINE_ARGS
  12. %x GNU_EXTENSION
  13.  
  14. %{
  15. /***************************************
  16.   $Header: /home/amb/cxref/RCS/parse.l 1.11 1996/02/24 14:53:38 amb Exp $
  17.  
  18.   C Cross Referencing & Documentation tool. Version 1.0
  19.  
  20.   C lexical analyser
  21.   ******************/ /******************
  22.   Original Written by N. A. Balharrie
  23.   CPP processing, GNU extensions and using yylval as a string Written by Andrew M. Bishop
  24.  
  25.   This file Copyright 1995,96 Andrew M. Bishop
  26.   It may be distributed under the GNU Public License, version 2, or
  27.   any higher version.  See section COPYING of the GNU Public license
  28.   for conditions under which this file may be redistributed.
  29.   ***************************************/
  30.  
  31. #include "parse-yy.h"
  32. #include "parse-yacc.h"
  33. #include "cxref.h"
  34. #include "memory.h"
  35.  
  36. /*+ If we are in a header file then ignore the comments. +*/
  37. extern int in_header;
  38.  
  39. /*+ Use all comments, not just those specially formatted +*/
  40. extern int option_all_comments;
  41.  
  42. /*+ The flags and filename that comes out of GCC when a file is included. +*/
  43. static int inc_file_flags=0;
  44. static char* inc_file_name=NULL;
  45.  
  46. /*+ The value of the thing that is defined (but only if it is simple). +*/
  47. static char* define_value=NULL;
  48.  
  49. /*+ The lex state at the time that a comment is seen. +*/
  50. static int comment_init_state=INITIAL;
  51.  
  52. /*+ To get around the GCC __extension__ keyword, pretend that it is a fuction call and skip over matched () counted by this. +*/
  53. static int gnu_ext_depth=0;
  54.  
  55. /*+ If we see a comment immediately after a ',', ';' '};', '},' or ')' then push it before. +*/
  56. static int push_past=0;
  57.  
  58. %}
  59.  
  60. %%
  61.  
  62. <*>"/*"                           { comment_init_state = YY_START; BEGIN(COMMENT_START); }
  63.  
  64. <COMMENT_START>"*"+[ \t\n]*       { BEGIN(SPECIAL_COMMENT); }
  65. <COMMENT_START>"+"+[ \t\n]*       { BEGIN(SPECIAL_COMMENT); }
  66. <COMMENT_START>[*+ \t\n]*"*/"     { BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  67. <COMMENT_START>[^*+]              { if(option_all_comments) BEGIN(SPECIAL_COMMENT); else BEGIN(COMMENT); }
  68.  
  69. <COMMENT>.|\n                     { }
  70. <COMMENT>[+*]+/"*/"               { fprintf(stderr,"cxref: Warning unbalanced comment; starts simple, ends special.\n"); }
  71. <COMMENT>"*/"                     { BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  72.  
  73. <SPECIAL_COMMENT>[ \t]+/\n        { }
  74. <SPECIAL_COMMENT>^[ \t]+          { }
  75. <SPECIAL_COMMENT>[^ \t\n*+]+      { if(!in_header) SeenComment(yytext); }
  76. <SPECIAL_COMMENT>.|\n             { if(!in_header) SeenComment(yytext); }
  77. <SPECIAL_COMMENT>[ \t\n]*"*"+"*/" { if(!in_header) SeenComment((char*)0);
  78.                                     BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  79. <SPECIAL_COMMENT>[ \t\n]*"+"+"*/" { if(!in_header) SeenComment((char*)1);
  80.                                     if(!in_header && comment_init_state==CPP_DEFINE_ARGS) SeenDefineFuncArgComment();
  81.                                     if(!in_header && comment_init_state==CPP_DEFINE_BODY) SeenDefineComment();
  82.                                     BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  83. <SPECIAL_COMMENT>[ \t\n]*"*/"     { if(!option_all_comments)
  84.                                       {
  85.                                        fprintf(stderr,"cxref: Warning unbalanced comment; starts special, ends simple.\n");
  86.                                        if(!in_header) SeenComment((char*)3);
  87.                                       }
  88.                                     else
  89.                                       {
  90.                                        if(!in_header) SeenComment((char*)2);
  91.                                        if(!in_header && comment_init_state==CPP_DEFINE_ARGS) SeenDefineFuncArgComment();
  92.                                        if(!in_header && comment_init_state==CPP_DEFINE_BODY) SeenDefineComment();
  93.                                       }
  94.                                     BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  95.  
  96. [ \t]*#[ \t]*                   { BEGIN(CPP_START); }
  97.  
  98. <CPP_START>{D}+[ \t]+"\""       { BEGIN(CPP_INC_FILE);}
  99. <CPP_START>define               { BEGIN(CPP_DEFINE); }
  100. <CPP_START>[a-z]+               { BEGIN(CPP); }
  101.  
  102. <CPP>.                          { }
  103. <CPP>\n                         { BEGIN(INITIAL); }
  104.  
  105. <CPP_DEFINE>{L}({L}|{D})*       { if(!in_header) SeenDefine(yytext); BEGIN(CPP_DEFINE_ARGP); }
  106. <CPP_DEFINE>\\\n                { }
  107. <CPP_DEFINE>[ \t]+              { }
  108.  
  109. <CPP_DEFINE_ARGP>"("            { BEGIN(CPP_DEFINE_ARGS);  }
  110. <CPP_DEFINE_ARGP>\n             { BEGIN(INITIAL);          }
  111. <CPP_DEFINE_ARGP>[^\n(]         { define_value=NULL; BEGIN(CPP_DEFINE_BODY);  }
  112. <CPP_DEFINE_ARGP>\\\n           { }
  113.  
  114. <CPP_DEFINE_ARGS>{L}({L}|{D})*  { if(!in_header) SeenDefineFunctionArg(yytext); }
  115. <CPP_DEFINE_ARGS>","            { }
  116. <CPP_DEFINE_ARGS>[ \t]+         { }
  117. <CPP_DEFINE_ARGS>\\\n           { }
  118. <CPP_DEFINE_ARGS>")"            { define_value=(char*)1; BEGIN(CPP_DEFINE_BODY); }
  119.  
  120. <CPP_DEFINE_BODY>{L}({L}|{D})*               { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  121. <CPP_DEFINE_BODY>0[xX]{H}+{IS}?              { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  122. <CPP_DEFINE_BODY>0{D}+{IS}?                  { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  123. <CPP_DEFINE_BODY>{D}+{IS}?                   { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  124. <CPP_DEFINE_BODY>'(\\.|[^\\'])+'             { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  125. <CPP_DEFINE_BODY>{D}+{E}{FS}?                { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  126. <CPP_DEFINE_BODY>{D}*"."{D}+({E})?{FS}?      { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  127. <CPP_DEFINE_BODY>{D}+"."{D}*({E})?{FS}?      { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  128. <CPP_DEFINE_BODY>\"(\\.|[^\\"])*\"           { if(!in_header) {if(!define_value) define_value=CopyString(yytext); else define_value=(char*)1;} }
  129. <CPP_DEFINE_BODY>.                           { }
  130. <CPP_DEFINE_BODY>\\\n                        { }
  131. <CPP_DEFINE_BODY>\n                          { if(define_value>(char*)1) SeenDefineValue(define_value); BEGIN(INITIAL); }
  132.  
  133. <CPP_INC_FILE>[^"]+             { inc_file_name=SeenCPPFilename(yytext); }
  134. <CPP_INC_FILE>"\""              { inc_file_flags=0; BEGIN(CPP_INC_FLAGS); }
  135. <CPP_INC_FLAGS>[ \t]+{D}+       { inc_file_flags+=1<<atoi(yytext); }
  136. <CPP_INC_FLAGS>[ \t]*"\n"       { if(inc_file_flags&6) SeenInclude(inc_file_name,inc_file_flags); BEGIN(INITIAL); }
  137.  
  138.  
  139. "auto"                  { yylval="auto"    ; return(AUTO);     }
  140. "break"                 { yylval="break"   ; return(BREAK);    }
  141. "case"                  { yylval="case"    ; return(CASE);     }
  142. "char"                  { yylval="char"    ; return(CHAR);     }
  143. "const"                 { yylval="const"   ; return(CONST);    }
  144. "continue"              { yylval="continue"; return(CONTINUE); }
  145. "default"               { yylval="default" ; return(DEFAULT);  }
  146. "do"                    { yylval="do"      ; return(DO);       }
  147. "double"                { yylval="double"  ; return(DOUBLE);   }
  148. "else"                  { yylval="else"    ; return(ELSE);     }
  149. "enum"                  { yylval="enum"    ; return(ENUM);     }
  150. "extern"                { yylval="extern"  ; return(EXTERN);   }
  151. "float"                 { yylval="float"   ; return(FLOAT);    }
  152. "for"                   { yylval="for"     ; return(FOR);      }
  153. "goto"                  { yylval="goto"    ; return(GOTO);     }
  154. "if"                    { yylval="if"      ; return(IF);       }
  155. "int"                   { yylval="int"     ; return(INT);      }
  156. "inline"                { yylval="inline"  ; return(INLINE);   }
  157. "long"                  { yylval="long"    ; return(LONG);     }
  158. "register"              { yylval="register"; return(REGISTER); }
  159. "return"                { yylval="return"  ; return(RETURN);   }
  160. "short"                 { yylval="short"   ; return(SHORT);    }
  161. "signed"                { yylval="signed"  ; return(SIGNED);   }
  162. "sizeof"                { yylval="sizeof"  ; return(SIZEOF);   }
  163. "static"                { yylval="static"  ; return(STATIC);   }
  164. "struct"                { yylval="struct"  ; return(STRUCT);   }
  165. "switch"                { yylval="switch"  ; return(SWITCH);   }
  166. "typedef"               { yylval="typedef" ; return(TYPEDEF);  }
  167. "union"                 { yylval="union"   ; return(UNION);    }
  168. "unsigned"              { yylval="unsigned"; return(UNSIGNED); }
  169. "void"                  { yylval="void"    ; return(VOID);     }
  170. "volatile"              { yylval="volatile"; return(VOLATILE); }
  171. "while"                 { yylval="while"   ; return(WHILE);    }
  172.  
  173. "__const"               { yylval="const"   ; return(CONST);    }
  174. "__inline"              { yylval="inline"  ; return(INLINE);   }
  175.  
  176. "__attribute__"[ \t]+\(*[a-z]+\)* { }
  177.  
  178. "__extension__"         { gnu_ext_depth=0; yylval="__extension__"; BEGIN(GNU_EXTENSION); return(IDENTIFIER); }
  179. <GNU_EXTENSION>"("      { if(gnu_ext_depth++==0) {yylval="(";                 return('(');} }
  180. <GNU_EXTENSION>")"      { if(--gnu_ext_depth==0) {yylval=")"; BEGIN(INITIAL); return(')');} }
  181. <GNU_EXTENSION>[^()]    { }
  182.  
  183. {L}({L}|{D})*           { yylval=CopyString(yytext);
  184.                           if(HaveISeenType(yytext))
  185.                              return(TYPE_NAME);
  186.                           else
  187.                              return(IDENTIFIER);
  188.                         }
  189.  
  190. 0[xX]{H}+{IS}?          { yylval=CopyString(yytext); return(LITERAL); }
  191. 0{D}+{IS}?              { yylval=CopyString(yytext); return(LITERAL); } 
  192. {D}+{IS}?               { yylval=CopyString(yytext); return(LITERAL); }
  193. '(\\.|[^\\'])+'         { yylval=CopyString(yytext); return(LITERAL); }
  194.  
  195. {D}+{E}{FS}?            { yylval=CopyString(yytext); return(LITERAL); }
  196. {D}*"."{D}+({E})?{FS}?  { yylval=CopyString(yytext); return(LITERAL); }
  197. {D}+"."{D}*({E})?{FS}?  { yylval=CopyString(yytext); return(LITERAL); }
  198.  
  199. \"(\\.|[^\\"])*\"       { yylval=CopyString(yytext); return(STRING_LITERAL); }
  200.  
  201. "..."            { yylval="...";  return(ELLIPSES);     }
  202. ">>="                   { yylval=">>=";  return(RIGHT_ASSIGN); }
  203. "<<="                   { yylval="<<=";  return(LEFT_ASSIGN);  }
  204. "+="                    { yylval="+=";   return(ADD_ASSIGN); }
  205. "-="                    { yylval="-=";   return(SUB_ASSIGN); }
  206. "*="                    { yylval="*=";   return(MUL_ASSIGN); }
  207. "/="                    { yylval="/=";   return(DIV_ASSIGN); }
  208. "%="                    { yylval="%=";   return(MOD_ASSIGN); }
  209. "&="                    { yylval="&=";   return(AND_ASSIGN); }
  210. "^="                    { yylval="^=";   return(XOR_ASSIGN); }
  211. "|="                    { yylval="|=";   return(OR_ASSIGN);  }
  212. ">>"                    { yylval=">>";   return(RIGHT_SHIFT);}
  213. "<<"                    { yylval="<<";   return(LEFT_SHIFT); }
  214. "++"                    { yylval="++";   return(INC_OP); }
  215. "--"                    { yylval="--";   return(DEC_OP); }
  216. "->"                    { yylval="->";   return(PTR_OP); }
  217. "&&"                    { yylval="&&";   return(AND_OP); }
  218. "||"                    { yylval="||";   return(OR_OP); }
  219. "<="                    { yylval="<=";   return(LE_OP); }
  220. ">="                    { yylval=">=";   return(GE_OP); }
  221. "=="                    { yylval="==";   return(EQ_OP); }
  222. "!="                    { yylval="!=";   return(NE_OP); }
  223. ";"[ \t]*"/*"           { yylval=";\n";  push_past=2; comment_init_state = YY_START; BEGIN(COMMENT_START); }
  224. ";"                     { yylval=";";    return(';'); }
  225. "{"                     { yylval="{";    return('{'); }
  226. "};"[ \t]*"/*"          { yylval="};\n"; push_past=3; comment_init_state = YY_START; BEGIN(COMMENT_START); }
  227. "},"[ \t]*"/*"          { yylval="},\n"; push_past=3; comment_init_state = YY_START; BEGIN(COMMENT_START); }
  228. "}"                     { yylval="}";    return('}'); }
  229. ","[ \t]*"/*"           { yylval=",\n";  push_past=2; comment_init_state = YY_START; BEGIN(COMMENT_START); }
  230. ","                     { yylval=",";    return(','); }
  231. ":"                     { yylval=":";    return(':'); }
  232. "="                     { yylval="=";    return('='); }
  233. "("                     { yylval="(";    return('('); }
  234. ")"[ \t]*"/*"           { yylval=")\n";  push_past=2; comment_init_state = YY_START; BEGIN(COMMENT_START); }
  235. ")"                     { yylval=")";    return(')'); }
  236. "["                     { yylval="[";    return('['); }
  237. "]"                     { yylval="]";    return(']'); }
  238. "."                     { yylval=".";    return('.'); }
  239. "&"                     { yylval="&";    return('&'); }
  240. "!"                     { yylval="!";    return('!'); }
  241. "~"                     { yylval="~";    return('~'); }
  242. "-"                     { yylval="-";    return('-'); }
  243. "+"                     { yylval="+";    return('+'); }
  244. "*"                     { yylval="*";    return('*'); }
  245. "/"                     { yylval="/";    return('/'); }
  246. "%"                     { yylval="%";    return('%'); }
  247. "<"                     { yylval="<";    return('<'); }
  248. ">"                     { yylval=">";    return('>'); }
  249. "^"                     { yylval="^";    return('^'); }
  250. "|"                     { yylval="|";    return('|'); }
  251. "?"                     { yylval="?";    return('?'); }
  252.  
  253. [ \t\v\n\f]             { /* Ignore all white space */ }
  254. .                       { /* Ignore bad characters  */ }
  255.  
  256. %%
  257.